package com.example.java.base.reflect;

import java.lang.reflect.Field;
/**
 * PropertyUtil:万能的setXxx()方法,当然getXxx()也行
 *
 * @author zhangxiaoxiang
 * @date 2020/11/3
 */
public class PropertyUtil {
	/**
	 *  per.setXxx(value); private String name;
	 * @param obj 操作对象
	 * @param propertyName 属性
	 * @param value 值
	 */
	public static void setProperty(Object obj,String propertyName,Object value)
			throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
		Class<? > clazz = obj.getClass() ;
		Field field = clazz.getDeclaredField( propertyName) ;
		field.setAccessible(true);
		field.set(obj, value);
	}
}
